package com.weilx959.spt.core.component;

import com.jfoenix.controls.JFXToggleButton;
import com.weilx959.spt.core.utils.FXMLUtil;
import com.weilx959.spt.core.utils.Try;
import javafx.beans.DefaultProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

/**
 * 开关控件
 *
 * @author weilx
 * @since 2024年6月3日
 */
@DefaultProperty("value")
public class ToggleButtonFormItemControl extends ParentFormItemControl<Boolean> {
    private static final String XML = "/assets/fxml/component/toggle-button-form-control.fxml";

    @FXML
    private Label labelControl;

    @FXML
    private JFXToggleButton toggleButtonControl;

    public ToggleButtonFormItemControl() {
        super();
        initialize();
    }

    @Override
    public Try<String> validate() {
        return new Try.Success<>(null);
    }

    public EventHandler<ActionEvent> getOnAction() {
        return toggleButtonControl.getOnAction();
    }

    public void setOnAction(EventHandler<ActionEvent> onAction) {
        toggleButtonControl.setOnAction(onAction);
    }

    private void initialize() {
        // load
        FXMLUtil.loadControl(this, XML);
        // init value
        oldValue = Boolean.FALSE;
        setValue(Boolean.FALSE);
        // toggle button
        toggleButtonControl.selectedProperty().bindBidirectional(valueProperty());
    }

    // builder
    public static ToggleButtonFormItemControlBuilder builder() {
        return new ToggleButtonFormItemControlBuilder();
    }

    public static class ToggleButtonFormItemControlBuilder
        extends ParentFormItemControlBuilder<Boolean, ToggleButtonFormItemControl, ToggleButtonFormItemControlBuilder> {
        @Override
        protected ToggleButtonFormItemControl newFormItemControl() {
            return new ToggleButtonFormItemControl();
        }

        @Override
        protected ToggleButtonFormItemControlBuilder self() {
            return this;
        }
    }
}
